package com.cheng.dreams.sample.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader;
import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.cheng.dreams.sample.R;
import com.cheng.dreams.sample.contract.home.HomeContract;
import com.cheng.dreams.sample.entity.response.HomeBannerEntity;
import com.cheng.dreams.sample.presenter.home.HomePresenter;
import com.cheng.dreams.sample.ui.adapter.HomeBannerHolderView;
import com.cheng.dreams.sample.ui.adapter.HomeShopAdapter;
import com.cheng.dreams.sample.util.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeFragment extends Fragment implements HomeContract.View {

    @BindView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;
    @BindView(R.id.headview)
    RecyclerViewHeader headview;
    @BindView(R.id.recyler)
    RecyclerView recyler;
    @BindView(R.id.search_rl)
    RelativeLayout searchRl;
    @BindView(R.id.message_iv)
    ImageView messageIv;
    HomeContract.Presenter presenter;
    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        presenter = new HomePresenter(this,getActivity());
        initView();
        return view;
    }

    public void initView() {
        presenter.onBanner();
        ArrayList<String> list = new ArrayList<>();
        list.add("1");
        list.add("1");
        list.add("1");
        list.add("1");
        recyler.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        recyler.addItemDecoration(new DividerGridItemDecoration(getActivity()));
        recyler.setItemAnimator(new DefaultItemAnimator());
        HomeShopAdapter adapter = new HomeShopAdapter(list);
        recyler.setAdapter(adapter);
        headview.attachTo(recyler);
        setBanner(new ArrayList<HomeBannerEntity.BannerEntity>());
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(int code, String msg) {

    }

    @Override
    public void onBannerSuccess(List<HomeBannerEntity.BannerEntity> entity) {
        setBanner(entity);
    }


    public void setBanner(List<HomeBannerEntity.BannerEntity> entity){
        entity.add(new HomeBannerEntity.BannerEntity("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"));
        entity.add(new HomeBannerEntity.BannerEntity("http://pic6.huitu.com/res/20130116/84481_20130116142820494200_1.jpg"));
        convenientBanner.setPages(
                new CBViewHolderCreator<HomeBannerHolderView>() {
                    @Override
                    public HomeBannerHolderView createHolder() {
                        return new HomeBannerHolderView();
                    }
                }, entity)
                .setPageIndicator(new int[]{R.drawable.unpage, R.drawable.onpage})
                .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL);
    }

}
